package com.example.final_project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class Play_Activity extends Activity implements OnClickListener, OnItemClickListener{
	Button btn_Main_menu;
	TextView lbl_Level;
	GridView gridView;
	int Level;
	Bundle extra_values;
	Integer[] Easy_Level = {R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb};
	
	Integer[] Medium_Level = {R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb,
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb};
	
	Integer[] Hard_Level = {R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb,
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb,
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb};
	
	// Hàm khởi tạo cho các biến Button
	private void Initial_Function()
	{
		btn_Main_menu=(Button)findViewById(R.id.btn_main_menu);
		gridView = (GridView)findViewById(R.id.grv_images);
		lbl_Level=(TextView)findViewById(R.id.lbl_level);
		extra_values=getIntent().getExtras();
		if(extra_values!=null)
		{
			Level=extra_values.getInt("Diff_Option");
		}
	}
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_play);
		Initial_Function();
		btn_Main_menu.setOnClickListener(this);
		gridView.setAdapter(new Adapter(this));
		gridView.setOnItemClickListener(this);
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_main_menu:
			AlertDialog dialogBox = makeAndShowDialogBox();
			dialogBox.show();
			break;
		}
	}
	
	// Tạo lớp Adapter
	private class Adapter extends BaseAdapter 
	{
		Context mContext;
		public Adapter(Context mContext){
			this.mContext = mContext;
		}
		// number of images
		@Override
		public int getCount() {
			if(Level==0)
				return Easy_Level.length;
			else if(Level==1)
				return Medium_Level.length;
			return Hard_Level.length;
		}
		// get items
		@Override
		public Object getItem(int position) {
			return null;
		}
		// get items 
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		// method getView
		@Override 
		public View getView(int position, View convertView, ViewGroup parent) { 
			ImageView mImage; 
			if (convertView == null) { 
				mImage = new ImageView(Play_Activity.this); 
				mImage.setLayoutParams(new GridView.LayoutParams(50, 50)); 
				mImage.setScaleType(ScaleType.FIT_XY); 
				convertView = mImage; } 
			else { 
				mImage = (ImageView) convertView; // show image if it has been loaded already
			} 
			if(Level==0)
				mImage.setImageResource(Easy_Level[position]);
			else if(Level==1)
				mImage.setImageResource(Medium_Level[position]); 
			if(Level==2)
				mImage.setImageResource(Hard_Level[position]); 
			return mImage; 
		} 
	}// Hết lớp Adapter
	
	@Override
	public void onItemClick( AdapterView<?> parent, View v, int position, long id) {
		ImageView Changed_Image = (ImageView) v;
		Changed_Image.setImageResource(R.drawable.on_bulb);
	}//onItemClick
	
	// Tạo AlertDialog
	private AlertDialog makeAndShowDialogBox(){
		AlertDialog myQuittingDialogBox =
		new AlertDialog.Builder(this)
		// thiết lập tiêu đề và thông điệp
		.setTitle("Termination")
		.setMessage("If you quit the game, you'll lose it. Do you still want to do this?")		
			
		//xử lý khi người chơi chọn YES
		.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				finish();
				return;
			}
		})
			// xử lý khi người chơi chọn NO
		.setNegativeButton("NO", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				return;
			}
		})
			
		.create();
		return myQuittingDialogBox;
	}
}
